package com.ruoyi.applet.service.impl;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.ChannelBaseInfo;
import com.ruoyi.db.mapper.ChannelBaseInfoMapper;
import com.ruoyi.laiqianba.constant.RedisKeyConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.AppletConfigInfoMapper;
import com.ruoyi.db.domain.AppletConfigInfo;
import com.ruoyi.applet.service.IAppletConfigInfoService;

import javax.annotation.Resource;

/**
 * 小程序配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-27
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class AppletConfigInfoServiceImpl implements IAppletConfigInfoService
{
    @Resource
    AppletConfigInfoMapper appletConfigInfoMapper;


    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 查询小程序配置
     * 
     * @param id 小程序配置主键
     * @return 小程序配置
     */
    @Override
    public AppletConfigInfo selectAppletConfigInfoById(Long id)
    {
        return appletConfigInfoMapper.selectAppletConfigInfoById(id);
    }

    /**
     * 查询小程序配置列表
     *
     * @param appletConfigInfo 小程序配置
     * @return 小程序配置
     */
    @Override
    public List<AppletConfigInfo> selectAppletConfigInfoList(AppletConfigInfo appletConfigInfo)
    {
        return appletConfigInfoMapper.selectAppletConfigInfoList(appletConfigInfo);
    }

    /**
     * 查询小程序配置列表
     *
     * @param appletConfigInfo 小程序配置
     * @return 小程序配置
     */
    @Override
    public List<Map<String, Object>> selectAppletConfigInfoMapList(AppletConfigInfo appletConfigInfo)
    {
        return appletConfigInfoMapper.selectAppletConfigInfoMapList(appletConfigInfo);
    }

    /**
     * 新增小程序配置
     * 
     * @param appletConfigInfo 小程序配置
     * @return 结果
     */
    @Override
    public AjaxResult insertAppletConfigInfo(AppletConfigInfo appletConfigInfo)
    {
        if(ObjectUtil.isEmpty(appletConfigInfo)){
            return AjaxResult.error("配置参数不能为空");
        }
//        ChannelBaseInfo channelBaseInfo = channelBaseInfoMapper.selectChannelBaseInfoByChannelCode(appletConfigInfo.getChannelCode());
//        if(channelBaseInfo == null){
//            return AjaxResult.error("该渠道编码不在渠道列表中");
//        }
//        AppletConfigInfo appletConfigInfo1 = appletConfigInfoMapper.selectAppletConfigInfoByChannelCode(appletConfigInfo.getChannelCode());
//        if (appletConfigInfo1 != null){
//            return AjaxResult.error("该渠道编码已经存在，不可重复添加");
//        }
        appletConfigInfo.setCreateTime(DateTime.now());
        return AjaxResult.success(appletConfigInfoMapper.insertAppletConfigInfo(appletConfigInfo));
    }

    /**
     * 修改小程序配置
     * 
     * @param appletConfigInfo 小程序配置
     * @return 结果
     */
    @Override
    public int updateAppletConfigInfo(AppletConfigInfo appletConfigInfo)
    {
        appletConfigInfo.setUpdateTime(DateUtils.getNowDate());
        int i = appletConfigInfoMapper.updateAppletConfigInfo(appletConfigInfo);
        if(i > 0){
            stringRedisTemplate.delete(RedisKeyConstant.APPLET_CONFIG+appletConfigInfo.getAppid());
        }
        return i;
    }

    /**
     * 批量删除小程序配置
     * 
     * @param ids 需要删除的小程序配置主键
     * @return 结果
     */
    @Override
    public int deleteAppletConfigInfoByIds(Long[] ids)
    {
        return appletConfigInfoMapper.deleteAppletConfigInfoByIds(ids);
    }

    /**
     * 删除小程序配置信息
     * 
     * @param id 小程序配置主键
     * @return 结果
     */
    @Override
    public int deleteAppletConfigInfoById(Long id)
    {
        return appletConfigInfoMapper.deleteAppletConfigInfoById(id);
    }

    @Override
    public  List<Map<String, Object>> selectAppletConfigInfoByH5MapList(AppletConfigInfo appletConfigInfo) {
        return appletConfigInfoMapper.selectAppletConfigInfoByH5MapList(appletConfigInfo);
    }

    @Override
    public int updateAppletInfo(AppletConfigInfo appletConfigInfo) {
        return appletConfigInfoMapper.updateAppletInfo(appletConfigInfo);
    }

    @Override
    public int updateAppletInfoByH5(AppletConfigInfo appletConfigInfo) {
        return appletConfigInfoMapper.updateAppletInfoByH5(appletConfigInfo);
    }

    @Override
    public List<Map<String, Object>> selectAppletConfigInfoByH5MapList1(AppletConfigInfo appletConfigInfo) {
        return appletConfigInfoMapper.selectAppletConfigInfoByH5MapList1(appletConfigInfo);
    }

    @Override
    public int updateAppletInfoByApp(AppletConfigInfo appletConfigInfo) {
        return appletConfigInfoMapper.updateAppletInfoByApp(appletConfigInfo);
    }
}
